package com.utilities.IO.fileWriters;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;

public class SplitScaffoldFile {
	static int counter = 0;
	private static PrintWriter pw = null;
	
	public static void main(String[] args) throws Exception
	{
		BufferedReader buff = new BufferedReader(new FileReader("C:/Users/bansalp/Desktop/scaffold.txt"));
		char[] readArray = new char[1000];
		int read = buff.read(readArray);
		StringBuffer strBuff = new StringBuffer();
		while(read != -1)
		{
			for (int i = 0; i < read; i++)
			{
				if (strBuff.length() == 60)
				{
					purge(strBuff.toString(), false);
					strBuff = new StringBuffer();
					strBuff.append(readArray[i]);
				}
				// just skip it
				else if (readArray[i] == '\n' || readArray[i] == '\r')
				{
//					purge(strBuff.toString(),true);
//					strBuff = new StringBuffer();
				}
				else if (readArray[i] == 'N')
				{
					purge(strBuff.toString(),true);
					strBuff = new StringBuffer();
				}
				else 
				{
					strBuff.append(readArray[i]);
				}
			}
			read = buff.read(readArray);
		}
		if (pw != null)
		{
			if (strBuff.length() > 0)
			{
				pw.println(strBuff.toString());
			}
			pw.flush();
			pw.close();
		}
	}

	private static void purge(String string, boolean createNew) throws Exception
	{
		if (pw == null)
		{
			pw = new PrintWriter("C:/Users/bansalp/Desktop/parit/" + counter + ".txt");
			pw.println(">contig" + counter);
		}
		if (createNew)
		{
			if (string.length() > 0)
			{
				pw.print(string + "\n");
				pw.flush();
				pw.close();
				counter++; 
				pw = new PrintWriter("C:/Users/bansalp/Desktop/parit/" + counter + ".txt");
				pw.println(">contig" + counter);
			}
		}
		else
		{
			pw.println(string);
		}
	}

}
